
package system.base;

import framework.core.*;
import system.data.*;
/*
	Author    :chengbiao.he
	CreateTime:2008-02-23 14:30:58
*/
public class BalanceSrv extends IHandler
{

	public BalanceSrv() 
	{
		super();
	}
	private void deleteBalance(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		Balance obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = Balance.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveBalance(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		Balance obj;
		if((id==null)||(id.equals("")))   //add
		{
			obj = Balance.fetch();
    	}
    	else                     //update
    	{
    		obj = Balance.fetch(con,id);
    	}
		BalancePageData data = (BalancePageData)PageData.fromForm(context,BalancePageData.class);
		context.setValue("BalancePageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listBalance(DBConnection con,SrvContext context,String path) throws Exception
	{
		BalanceSearchEvent event = (BalanceSearchEvent)SearchEvent.fromForm(context,BalanceSearchEvent.class);
		context.setSessionValue("BalanceSearchEvent",event);
		context.setNextUrl(path+"BalanceList.jsp");
	}
	private void backBalance(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"BalanceList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listBalance"))
		{
			listBalance(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewBalance"))
		{
			String id = context.getString("Id",null);
			Balance obj = Balance.fetch(con,id);
			context.setValue("Balance",obj);
			context.setNextUrl(path+"BalanceView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteBalance"))
		{
			context.setErrorUrl(path+"BalanceList.jsp");
			deleteBalance(con,context);
			backBalance(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddBalance"))
		{
			Balance obj = Balance.fetch();
			BalancePageData data = (BalancePageData)PageData.fromTable(obj,BalancePageData.class);
			context.setValue("BalancePageData",data);
			context.setNextUrl(path+"BalanceEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateBalance"))
		{
			String id = context.getString("Id",null);
			Balance obj = Balance.fetch(con,id);
			BalancePageData data = (BalancePageData)PageData.fromTable(obj,BalancePageData.class);
			context.setValue("BalancePageData",data);
			context.setNextUrl(path+"BalanceEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyBalance"))
		{
			String id = context.getString("Id",null);
			Balance obj = Balance.copy(Balance.fetch(con,id));
			BalancePageData data = (BalancePageData)PageData.fromTable(obj,BalancePageData.class);
			context.setValue("BalancePageData",data);
			context.setNextUrl(path+"BalanceEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveBalance"))
		{
			context.setErrorUrl(path+"BalanceEdit.jsp");
			saveBalance(con,context,loginContext);
			backBalance(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewBalance"))
		{
			context.setErrorUrl(path+"BalanceEdit.jsp");
			saveBalance(con,context,loginContext);
			Balance obj = Balance.fetch();
			BalancePageData data = (BalancePageData)PageData.fromTable(obj,BalancePageData.class);
			context.setValue("BalancePageData",data);
			context.setNextUrl(path+"BalanceEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListBalance"))
		{
			backBalance(con,context,path);
			return;
		}
		
		//add by chengbiao.he start

		//add by chengbiao.he end

		Logger.throwException("BalanceSrv","unknown action_code="+action_code);
	}
}
